// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    pageLoading: false,
    loadMore: false,
    noMore: false,
    page: 1,
    pageSize: 10,
    adList: [
      {
        "id": "1",
        "imageUrl": "https://img1.baidu.com/it/u=735578287,2053816341&fm=253&fmt=auto&app=138&f=JPEG?w=674&h=500"
      },
      {
        "id": "2",
        "imageUrl": "https://img2.baidu.com/it/u=3776226724,823769551&fm=253&fmt=auto&app=138&f=JPEG?w=753&h=500"
      }
    ],
    tabs: [
      { id: 0, text: '夏日防晒' },
      { id: 1, text: '二胎大作战' },
      { id: 2, text: '人气榜' },
      { id: 3, text: '好评榜' },
      { id: 4, text: '手机也疯狂' },
      { id: 5, text: '附近热卖' },
    ],
    goodList: [
      {
        id: 0, name: '裙子加快速度放假啦司法拘留苏卡达飞机卡手就开始进度款发件方', tag: '2020秋季新款2020秋季新款2020秋季新款2020秋季新款2020秋季新款', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
      {
        id: 1, name: '裙子1', tag: '限时特惠', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
      {
        id: 2, name: '裙子2', tag: '限时特惠', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
      {
        id: 3, name: '裙子3', tag: '限时特惠', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
      {
        id: 4, name: '裙子4', tag: '限时特惠', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
      {
        id: 5, name: '裙子5', tag: '限时特惠', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
      {
        id: 6, name: '裙子6', tag: '限时特惠', salePrice: 300,
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
    ],
    currentIndex: 0
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init();
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function(){
    this.setData({
      noMore: false,
      pageLoading: true,
    });
    setTimeout(() => {
      this.setData({
        pageLoading: false,
        loadMore: false
      });
      var newItems = [];
      for (let index = 0; index < 8; index++) {
        newItems[index] = {
            id: 0, name: '裙子'+index, tag: '2020秋季新款', salePrice: 300, originalPrice: 350, 
          imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
        };
      }
      this.setData({
        goodList: newItems,
        noMore: newItems.length < 10,
        page: 1
      });
      wx.stopPullDownRefresh(); // 停止下拉刷新
    }, 1000);
  },

  onReachBottom(){
    console.log('加载更多');
    this.setData({
      loadMore: true,
      noMore: false,
      page: this.data.page + 1
    });
    var newItems = [];
    var noMoreTemp = false;
    if(this.data.page < 3){
      for (let index = 0; index < this.data.pageSize; index++) {
        newItems[index] = {
            id: 0, name: '裙子'+(this.data.pageSize * this.data.page + index + 1), tag: '2020秋季新款', salePrice: 300, originalPrice: 350, 
          imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
        };
      }
    }else{
      noMoreTemp = true;
    }
    setTimeout(() => {
      this.setData({
        loadMore: false,
        noMore: noMoreTemp,
        goodList: this.data.goodList.concat(newItems)
      });
    }, 2000);
  },
  
  searchClick(e){
    console.log('搜索点击');
    wx.navigateTo({
      url: '/pages/goods/search/index',
    })
  },
  handleChange(e) {
    const index = e.detail.index;
    //页签切换
    
  },
  goodsDetail(e){
    const goodsId = e.currentTarget.dataset.goodsId;
    console.log(goodsId);
    wx.navigateTo({
      url: `/pages/goods/detail/index?goodsId=${goodsId}`
    });
  },
  cartAdd(e){
    wx.showToast({
      title: '添加到购物车',
    })
  },
})
